package io.ukoko.controller;

import io.ukoko.dto.Result;
import io.ukoko.entity.Book;
import io.ukoko.service.BookService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
public class BookController {

    @Resource
    private BookService bookService;

    /**
     * 添加图书
     */
    @PostMapping(value = "/addBook")
    public Result addBook(@RequestBody Book book){
        bookService.addBook(book);
        return Result.ok();
    }
    /**
     * 删除图书(单个)
     */
    @GetMapping(value = "/deleteBook")
    public Result deleteBook(Integer bookId){
        bookService.deleteBookByBookId(bookId);
        return Result.ok();
    }
    /**
     * 删除图书(批量)
     */
    @PostMapping(value = "/batchDeleteBook")
    public Result batchDeleteBook(@RequestBody List<Integer> bookIds){
        bookService.batchDeleteBook(bookIds);
        return Result.ok();
    }
    /**
     * 更新图书
     */
    @PostMapping(value = "/updateBook")
    public Result updateBook(@RequestBody Book book){
        bookService.updateBook(book);
        return Result.ok();
    }
    /**
     * 分页搜索查询
     */
    @GetMapping(value = "/getBooksPage")
    public Result getBooksPage(@RequestParam(value = "pageNo") Integer pageNo,
                               @RequestParam(value = "pageSize") Integer pageSize,
                               String bookName,
                               Integer categoryId,
                               Integer status,
                               @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date start,
                               @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date end){

        return Result.ok(bookService.getBooksPage(pageNo,pageSize,bookName,categoryId,status,start,end));
    }
}
